package com.webtest.checkcodetest;

import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2018/8/8 18:50
 */
public class CheckServlet extends HttpServlet {
    private static final long serialVersionUID = -7314473716533183373L;

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1.获取请求参数
        String code = req.getParameter("code");
        String name = req.getParameter("name");
        // 2.获取session中的验证码
        String picCode = (String) req.getSession().getAttribute("piccode");
        // 3.比对，看是否一致，若一致，说明验证码正确，若不一致，说明验证码错误
        if (StringUtils.isEmpty(code) || !code.equals(picCode)) {
            req.setAttribute("message", "您输入的验证码错误，请重新输入！");
            req.setAttribute("oldName", name);
            req.getRequestDispatcher("/check/index.jsp").forward(req, resp);
            return;
        }
        req.getRequestDispatcher("/check/success.jsp").forward(req, resp);
    }
}
